<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

    <script>
        //基本数据类型 赋值
        let a = 5;
        let b = a;
        b = 3;
        console.log(a, b)

        //数组和对象的赋值都叫做浅拷贝
        let arr = [1, 2, 3];
        let newArr = arr;
        newArr.push("4")
        console.log(arr, newArr)

        //结构赋值对一维数组为深拷贝 对二维数组为浅拷贝
        let arr2 = [1, 2, 3]
        let newArr2 = [...arr2]
        newArr2.push(4);
        console.log(arr2, newArr2)
        //结构赋值的浅拷贝
        let arr3 = [
            [1, 2, 3],
            [4, 5, 6]
        ]
        let newArr3 = [...arr3]
        newArr3[0].push(888)
        console.log(arr3, newArr3)

        //真正的深拷贝百分之八十
        let list = [{
            id: 1,
            name: "小红",
            clas: "一班"
        }, {
            id: 1,
            name: "小白",
            clas: "二班"
        }, {
            id: 1,
            name: "小蓝",
            clas: "三班"
        }]
        let newList = JSON.parse(JSON.stringify(list)) //深拷贝
        newList.push({
            id: 88888
        })
        console.log(list, newList)

        //真正的深拷贝
        function deepClone(source) {
            //[]=>Array(基类) {}=>object
            const targetObj = source.constructor === Array ? [] : {}
            for (let keys in source) {
                if (source.hasOwnProperty(keys)) {
                    //keys=>3
                    //引用数据类型
                    if (source[keys] && typeof source[keys] === "object") {
                        //维护层代码
                        targetObj[keys] = source[keys].constructor === Array ? [] : {};
                        //递归
                        targetObj = deepClone(source[keys])
                    } else {
                        //不是obj直接赋值
                        targetObj[keys ] = source[keys];
                    }
                }
            }
        }
        let objC = {
            ff: "name",
            gg: 1,
            obj: {
                str: "111",
                age: "12"
            },
            arr: [1, 2, 3]
        }
    </script>
</body>

</html>